// pages/fuyaotixingdetail/fuyaotixingdetail.js
const app = getApp()
const util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    drugList: [],
    list: [],
    activeNames: ['1'],
    type:0,
    isdrug:1,
    date:null
  },

  onChange(event) {
    this.setData({
      activeNames: event.detail,
    });
  },
  previewImg: function (e) {
    var index = e.currentTarget.dataset.index;
    console.log(index)
    var a = index.split(",")
    var url = a[1]
    var that = this;
    var imgArr = []
    for (var i = 0; i < that.data.drugList[a[0]].Attachments.length; i++) {
      imgArr.push(that.data.drugList[a[0]].Attachments[i].attachmentUrl)
    }
    wx.previewImage({
      current: url, //当前图片地址
      urls: imgArr, //所有要预览的图片的地址集合 数组形式
      success: function (res) {},
      fail: function (res) {},
      complete: function (res) {},
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var list = JSON.parse(options.mess)
    var drugList = list.drugList
    this.setData({
      list: list,
      drugList: drugList,
      type:options.type,
      date:options.date
    })

  },

  sendClick: function (e) {
  
      this.postData()
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    wx.setNavigationBarTitle({
      title: this.data.list.kidName+"服药详情",
      success: (res) => {},
      fail: (res) => {},
      complete: (res) => {},
    })
  },


  postData: function () {

  var drugstr=""
  for(var i=0;i<this.data.drugList.length;i++){
    drugstr+=","+this.data.drugList[i].drugID
  }
  if(drugstr.length>0){
    drugstr=drugstr.substring(1)
  }

    var that = this;
    var data = {
      "userid": app.globalData.userId,
      "schoolid": app.globalData.schoolId,
      "isdrug": this.data.isdrug,
      "drugidStr": drugstr,
      "date": this.data.date,
      "appType": app.globalData.appType,
      "appKey": app.globalData.appKey,
      "timeSpan": app.globalData.timespan,
      "mobileSerialnumber": ""
    }
    wx.request({
      url: app.globalData.serverUrl,
      data: {
        "data": JSON.stringify(data),
        'act': "send_drugstatus"
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      success: function (res) {
        console.log(res);
        if (res.data.status == 0) {
          that.setData({
            type:2
          })        
        wx.showToast({
          title: "服药成功",
          icon: 'none',
          duration: 1000,
          mask: true
        })
          //数据清除
        } else {
          wx.showToast({
            title: "服药失败",
            icon: 'none',
            duration: 1000,
            mask: true
          })



        }
      },
      fail: function (res) {

        wx.showToast({
          title: "服药失败",
          icon: 'none',
          duration: 1000,
          mask: true
        })


      },
      complete: function (res) {
        console.log('submit complete');
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },


})